// pages/byorder/byorder.js
const app = getApp();

Page({
  data: {
    goods:[],
    basic :[],
    types:0,
    cpecids:'',
    num:0,
    cartids:'',
    bid :0,
  },
  testSubmit: function (e) {
    var that    = this;
    var types   = that.data.types;
    var uid     = wx.getStorageSync('uid');
    var total   = e.detail.target.dataset.total;
    var addrid  = e.detail.target.dataset.addrid;
    var remark  = e.detail.value.address;
    var formid  = e.detail.formId;
    var openid  = wx.getStorageSync('openid');
    if (types == 1){
      var spec_ids = that.data.specids;
      var num      = e.detail.target.dataset.num;
      var bid      = that.data.bid;
      if (bid == false || bid == null || bid == undefined || bid == ''){
        bid =0;
      }
      var price = e.detail.target.dataset.price;
      wx.request({
        url    : app.url + '/Order/submit_order',
        method : 'post',
        data   : {
          spec_ids  : spec_ids,
          num       : num,
          uid       : uid, 
          price     : price,
          total     : total ,
          bid       : bid,
          remark    : remark,
          formid    : formid,
        },
        success: function (res) {
          if (res.data.code == 1) {
            wx.request({
              url    : app.url + '/Pay/wxpay',
              data   : { order_sn: res.data.data },
              method : 'post',
              success: function (res) {
                console.log(res)
                if (res) {
                  wx.requestPayment({
                    timeStamp : res.data.timeStamp,
                    nonceStr  : res.data.nonceStr,
                    package   : res.data.package,
                    signType  : res.data.signType,
                    paySign   : res.data.paySign,
                    success   : function (pay) {
                      wx.showToast({  title: '支付成功',})
                      wx.navigateTo({ url: '../order/order'})
                      wx.request({
                        url: app.url + '/User/message_push',
                        data: { prepay_id: res.data.package, openid: openid, bid:bid},
                        method :'post',
                        success :function(res){
                          console.log(res)
                        },
                        error:function(){}
                      })
                    },
                    fail : function () {
                      wx.showToast({  title: '支付失败', })
                      wx.navigateTo({ url: '../order/order'})
                    },
                    complete: function () {
                      wx.navigateTo({ url: '../order/order' })
                    }
                  })
                }
              },
              error: function (err) { }
            })
          } else {
            return false;
          }
        },
        error: function () { }
      })
    }else{  //购物车提交订单
      var cartsid = that.data.cartids;
      wx.request({
        url: app.url + '/Order/cart_order',
        method  : 'post',
        data: { cartsid: cartsid,uid :uid ,total :total,addrid :addrid,remark:remark},
        success : function (res) {


          wx.request({
            url: app.url + '/Pay/wxpay',
            data: { order_sn: res.data.data },
            method: 'post',
            success: function (res) {
              if (res) {
                wx.requestPayment({
                  timeStamp: res.data.timeStamp,
                  nonceStr: res.data.nonceStr,
                  package: res.data.package,
                  signType: res.data.signType,
                  paySign: res.data.paySign,
                  success: function (pay) {
                    wx.showToast({ title: '支付成功', })
                    wx.navigateTo({ url: '../order/order' })
                  },
                  fail: function () {
                    wx.showToast({ title: '支付失败', })
                    wx.navigateTo({ url: '../order/order' })
                  },
                  complete: function () {
                    wx.navigateTo({ url: '../order/order' })
                  }
                })
              }
            },
            error: function (err) { }
          })
        },
        error   : function(){}
      })
    }
  },
  onLoad: function (options) {
    var that  = this;
    var uid   = wx.getStorageSync('uid');
    var type  = options.type;
    var bid   = options.bid
    if (bid){bid = bid }else{bid =0}
    that.setData({ types: type ,bid :bid})
    if(type == 1){
      var spec = options.num;
      var num = options.quantity;
      wx.request({
        url: app.url + '/Order/order_show',
        method: 'post',
        data: { spec: spec,uid: uid, num: num ,bid :bid},
        success: function (res) {
          if (res.data.code == 1) {
            that.setData({
              goods: res.data.data.goods,
              basic: res.data.data.basic,
              specids: res.data.data.goods[0].spec+',',
              num : num,
            })
          } else {
            return false;
          }
        },
        error: function (err) { wx.showToast({ title: '请求错误', }) }
      });
    }else{
      var cartids = options.cartids;
      that.setData({
        cartids: cartids
      })
      wx.request({
        url     : app.url + '/Order/cart_show',
        method  : 'post',
        data    : { cartsid: cartids, uid: uid },
        success: function (res) {
          that.setData({
            goods: res.data.data.goods,
            basic: res.data.data.basic,
          })
        },
        error: function () { }
      });
    }
  },
  onShow: function () {

  },
  start_ads: function () {
    wx.navigateTo({ url: '../myads/myads' })
  },
  onReady: function () {

  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
  
  }
})